/**
 * https://leetcode-cn.com/problems/convert-binary-number-in-a-linked-list-to-integer/
 * 二进制链表转整数
 * @param head 
 */
const getDecimalValue = (head: ListNode | null): number => {
    if (head == null) return 0
    let t = 0
    while (head !== null) {
        // 利用连乘的技巧
        t = (t << 1) + head.val;
        head = head.next;
    }

    return t;
};

// 10011
// 1 * 2 ^ 0 + 1 * 2 ^ 1 + 1 * 2 ^ 2 + 1 * 2 ^ 3 + 1 * 2 ^ 4
// 1 + 2 * (1 + 2 * (1 + 2 * (1 + 2 * 1))

const getDecimalValue2 = (head: ListNode | null): number => {
    if (head == null) return 0

    // 存储二进制数字
    let str = ''

    // 遍历
    while (head !== null) {
        str += head.val
        head = head.next;
    }

    // 从后往前遍历str
    let len = str.length - 1,
        n = 0,
        total = 0;
    for (let i = len; i >= 0; i--) {
        total += (+str[i]) * (2 ** (n++))
    }
    return total
};